<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()" ref="dataForm">
      <el-form-item prop="terminalPushState">
        <el-select v-model="dataForm.terminalPushState" placeholder="请选择是否推送">
          <el-option
            v-for="item in useroption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="noticeName">
        <el-input v-model="dataForm.noticeName" placeholder="请输入通知名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="search()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
        >批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="noticeName" header-align="center" align="center" label="通知名称"></el-table-column>
      <el-table-column prop="jumpLink" header-align="center" align="center" label="跳转链接"></el-table-column>
      <el-table-column prop="noticeType" header-align="center" align="center" label="通知操作">
        <template slot-scope="scope">
          {{changeMessType(scope.row.noticeType)}}
        </template>
      </el-table-column>
      <el-table-column prop="iconName" header-align="center" align="center" label="icon名称"></el-table-column>
      <el-table-column header-align="center" align="center" label="icon图片">
        <template slot-scope="scope">
          <img style="width: 50px;height: 50px;" :src="scope.row.iconUrl" alt="">
        </template>
      </el-table-column>
      <el-table-column prop="terminalPushState" header-align="center" align="center" label="是否推送">
        <template slot-scope="scope">{{scope.row.terminalPushState==0?'不推送':'推送'}}</template>
      </el-table-column>
      <el-table-column header-align="center" align="center" label="状态" prop="forbidState">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.forbidState"
            :active-value="0"
            :inactive-value="1"
            @change="batchAddView(scope.row.id,scope.row.forbidState)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <div v-bind:class="scope.row.pushRate!=1||scope.row.pushState=='1'?'active':'disable'">
            <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
            <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <notice-add v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></notice-add>
  </div>
</template>
<style>
.disable {
  pointer-events: none;
  color: #ccc;
  text-decoration: underline;
  margin-right: 5px;
}
.disable .el-button--text {
  color: #ccc;
}
.active {
  cursor: pointer;
}
</style>

<script>
import NoticeAdd from "./noticeAdd";
export default {
  data() {
    return {
      steward: [],
      useroption: [
        {
          value: "1",
          label: "推送"
        },
        {
          value: "0",
          label: "不推送"
        }
      ],
      dataForm: {
        id: "",
        noticeName: "",
        terminalPushState: ""
      },
      forbidState: "",

      isAlive: "",
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
      // show: "",
      // hide:"",
    };
  },
  components: {
    NoticeAdd
  },
  activated() {
    this.getDataList();
    this.initMessageType();
  },
  methods: {
    initMessageType(){
      this.$http({
        url: this.$http.adornUrl(`/user/noticetemplate/getNoticeType`),
        method: "get",
      }).then(({ data }) => {
        this.steward = data
      })
    },
    changeMessType(type) {
      let data = ''
      this.steward.map((item)=> {
        if(item.value == type) {
          data =  item.name
        }
      })
      return data
    },
    search(){
      this.pageIndex = 1;
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      var obj = {
        page: this.pageIndex,
        limit: this.pageSize
      };
      if (this.dataForm.noticeName) {
        obj.noticeName = this.dataForm.noticeName;
      }
      if (this.dataForm.terminalPushState) {
        obj.terminalPushState = this.dataForm.terminalPushState;
      }
      this.$http({
        url: this.$http.adornUrl("/user/noticetemplate/list"),
        method: "get",
        params: this.$http.adornParams(obj)
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$refs["dataForm"].resetFields();
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      console.log(id);
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm(
        `确定进行[${id ? "删除" : "批量删除"}]操作?`,
        // `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/user/noticetemplate/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
    batchAddView(id, forbidState) {
      console.log(forbidState);
      var obj = {};
      obj.id = id;
      obj.forbidState = forbidState;
      this.$http({
        url: this.$http.adornUrl("/user/noticetemplate/update"),
        method: "post",
        data: this.$http.adornData(obj)
      })
        .then(({ data }) => {
          if (data.code == 500) {
            this.$message.error(data.msg);
          }
          this.getDataList();
        })
        .catch(e => {
          this.$message.error(data.msg);
        });
    }
  }
};
</script>


